package com.fightingFish.user.servlet;

import com.fightingFish.user.bean.User;
import com.fightingFish.user.service.AttentionService;
import com.fightingFish.user.service.impl.AttentionServiceImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet("/attention")
public class AttentionServlet extends HttpServlet {
   private AttentionService as = new AttentionServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        String methodName = req.getParameter("methodName");
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }



    /**
     * 方法概述： 取消关注
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void cancelAttention(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("loginUser");
        int concernId = 0;
        int beConcernedId = 0;
        try {
            concernId = user.getUserId();
            beConcernedId = Integer.parseInt(req.getParameter("anchorId"));
        } catch (Exception e) {
        }
        if (concernId == 0) {
            JSONObject json = new JSONObject();
            json.put("result", "未登陆");
        }

        //调用方法查询该用户关注的用户信息
        boolean flag = as.cancelAttention(concernId, beConcernedId);
        resp.sendRedirect(req.getContextPath() + "/anchorSearch?methodName=queryAttentionAnchor");
    }


}
